package GradeManager.StudentGradeSystem.controller;

import GradeManager.StudentGradeSystem.pojo.Account;
import GradeManager.StudentGradeSystem.pojo.Result;
import GradeManager.StudentGradeSystem.service.AccountService;
import GradeManager.StudentGradeSystem.utils.JwtUtils;
import GradeManager.StudentGradeSystem.utils.MessageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/accounts")
public class AccountController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private JwtUtils jwtUtils;
    @GetMapping("/check/userId")
    public Result checkuserid(Integer id){
        Account account = accountService.checkUserId(id);
        if(null==account){
            return Result.success();
        }else {
            return Result.error("此用户已注册");
        }
    }
    @GetMapping("/check/studentId")
    public Result checkstudentid(Integer id){
        Account account = accountService.checkUserId(id);
        if(null==account){
            return Result.error("此学号未注册！");
        }else {
            if(account.getStatus()==2){
                return Result.error("不能为老师添加成绩！");
            }
            return Result.success();
        }
    }
    @GetMapping("/check/idStatus")
    public Result checkidststus(Integer id){
        Account account = accountService.checkUserId(id);
        if(null==account){
            return Result.error(MessageUtils.NOT_REGISTER);
        }else{
            if(account.getStatus()==1){
                return Result.success(1);
            }else if(account.getStatus()==2){
                return Result.success(2);
            }
        }
        return Result.success();
    }
    @PostMapping("/register")
    public Result register(@RequestBody Account account){
        boolean registSuccess = accountService.addAccount(account);
//        判断是否注册成功
        if(registSuccess){
            return Result.success();
        }else{
            //注册不成功
            return Result.error(MessageUtils.REGISTER_ERROR);
        }
    }

    @PostMapping("/login")
    public Result login(@RequestBody Account loginAccount){
        Result loginStatus = accountService.login(loginAccount);
        if (loginStatus.getCode()==1){
            Account loginInfo = (Account) loginStatus.getData();
            Map<String,Object> claim=new HashMap<>();
            claim.put("loginId",loginInfo.getUserId());
            claim.put("loginName",loginInfo.getUserName());
            claim.put("loginStatus",loginInfo.getStatus());
            String Jwt = jwtUtils.JwtBuilder(claim);
            log.info("登陆成功！");
            return Result.success(Jwt);
        }else return loginStatus;
    }
    @GetMapping("/check/codeImg")
    public Result checkCodeImg(HttpServletResponse resp) throws IOException {
        accountService.checkCodeImg(resp);
        return Result.success();
    }
    @GetMapping("/exitAccount")
    public Result exitAccount(){
        accountService.exitAccount();
        return Result.success();
    }
    @DeleteMapping("/destroyAccount")
    public Result destroyAccount(){
        accountService.destroyAccount();
        return Result.success();
    }
}
